package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwakT;
import cn.e4j.modular.crm.entity.GwalT;
import cn.e4j.modular.crm.entity.MmrwT;
import cn.e4j.modular.crm.entity.MmrxT;
import cn.e4j.modular.crm.service.GwerTService;
import cn.e4j.modular.crm.service.impl.MmrwTServiceImpl;
import cn.e4j.modular.crm.service.impl.MmrxTServiceImpl;
import cn.e4j.modular.erp.controller.ErpLogic;
import cn.e4j.modular.erp.service.impl.GPServiceImpl;
import cn.e4j.modular.erp.service.impl.YFServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 维修费用收入
 * Created by miaoyl on 2019-08-30
 */
@Controller
@RequestMapping("/crm/gwei025")
public class Gwei025Controller extends BaseController {

    @Autowired
    private MmrxTServiceImpl mmrxTService;

    @Autowired
    private ErpLogic erpLogic;

    /**
     * 跳转维修收入结算管理页面
     */
    @RequiresPermissions("crm:gwei025:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei025.html";
    }

    /**
     * 查询维修收入结算信息列表
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("crm:gwei025:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwei025:add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrxT> mmrxTList = inDto.getReqList(MmrxT.class);
        mmrxTService.addData(mmrxTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwei025:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrxT> mmrxTList = inDto.getReqList(MmrxT.class);
        mmrxTService.editData(mmrxTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwei025:del")
    @ResponseBody
    public ResponseData del(MmrxT mmrxT) {
        mmrxTService.delete(mmrxT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwei025:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrxT> mmrxTList = inDto.getReqList(MmrxT.class);
        mmrxTService.delDataBatch(mmrxTList);
        return SUCCESS_TIP;
    }


    /**
     * 修改状态
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("crm:gwei025:init")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrxT> mmrxTList = inDto.getReqList(MmrxT.class);
        mmrxTService.updateStatus(mmrxTList);
        return SUCCESS_TIP;
    }

    /**
     * 报工信息开窗
     */
    @RequestMapping("/getMmrhgiData")
    @ResponseBody
    public ResponseData getMmrhgiData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.getMmrhgiData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增数据
     */
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        outDto.put("MMRXDOCDATE", DateUtils.formatDate(new Date(), DateUtils.PATTERN_yyyy_MM_dd));
        outDto.put("MMRXDOCNO", "PSRD" + DateUtils.getDate("yyyyMMddHHmmssSS"));
        return ResponseData.success(outDto);
    }

    /**
     * 根据编号获取信息
     */
    @RequestMapping("/getMmrxData")
    @ResponseBody
    public ResponseData getMmrxData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.getMmrxData(inDto);
        return ResponseData.success(result);
    }

    /**
     * 获取ERP组织名称
     */
    @RequestMapping("/getMB002")
    @ResponseBody
    public ResponseData getMB002(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = mmrxTService.getMB002(inDto);
        return ResponseData.success(result);
    }

    /**
     * 获取总金额
     */
    @RequestMapping("/getMmrx005Sum")
    @ResponseBody
    public ResponseData getMmrx005Sum(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = mmrxTService.getMmrx005Sum(inDto);
        return ResponseData.success(result);
    }

    /**
     * 报工单转入
     */
    @RequestMapping("/transferMmrghiData")
    @RequiresPermissions("crm:gwei025:init")
    @ResponseBody
    public ResponseData transferMmrghiData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<MmrxT> mmrxTList = JSONObject.parseArray(jsonObject.getString("list"), MmrxT.class);
        inDto.put("gwaklist", jsonObject.getString("gwaklist"));
        Dto result = mmrxTService.transferMmrghiData(mmrxTList, inDto);
        return ResponseData.success(result);
    }

    /**
     * 转erp订单
     */
    @RequestMapping("/transferERP")
    @RequiresPermissions("crm:gwei025:init")
    @ResponseBody
    public ResponseData transferERP(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        //JSONArray result = mmrxTService.transferERP(inDto);
        JSONArray result = new JSONArray();
        try {
            result = erpLogic.getERPService("transfer025",inDto);
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseData.success(result);
    }

    /**
     * 还原erp订单
     */
    @RequestMapping("/restoreERP")
    @RequiresPermissions("crm:gwei025:restoreERP")
    @ResponseBody
    public ResponseData restoreERP(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        //Dto result = mmrxTService.restoreERP(inDto);
        Dto result = Dtos.newDto();
        try {
            result = erpLogic.getERPService("restore025",inDto);
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseData.success(result);
    }

    /**
     * 最终用户开窗
     */
    @RequestMapping("/listGwak")
    @ResponseBody
    public ResponseData listGwak(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.listGwak(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 法人开窗
     */
    @RequestMapping("/getSysOrg")
    @ResponseBody
    public ResponseData getSysOrg(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.getSysOrg(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取送货地址
     */
    @RequestMapping("/listGwal")
    @ResponseBody
    public ResponseData listGwal(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = mmrxTService.listGwal(inDto);
        return ResponseData.success(result);
    }

    /**
     * 获取签约单位
     */
    @RequestMapping("/getMmrx019")
    @ResponseBody
    public ResponseData getMmrx019(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.getMmrx019(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 销售人员
     */
    @RequestMapping("/getUserData")
    @ResponseBody
    public ResponseData getUserData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.listUser(page, inDto);
        return new PageReData(page, result);
    }

    //根据销售人员获取法人等信息
    @RequestMapping("/getOrg")
    @ResponseBody
    public ResponseData getOrg(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = mmrxTService.getOrg(inDto);
        return ResponseData.success(result);
    }

    /**
     * 销售人员
     */
    @RequestMapping("/getOrgData")
    @ResponseBody
    public ResponseData getOrgData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.getOrgData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 销售人员
     */
    @RequestMapping("/getGweaData")
    @ResponseBody
    public ResponseData getGweaData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrxTService.listGwea(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取折扣率、汇率类型、汇率
     */
    @ResponseBody
    @RequestMapping("getHuilvZhekou")
    public ResponseData getHuilvZhekou(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        Dto resultDto = this.mmrxTService.getMmrx010(inDto);
        Dto hulvTypeDto = this.mmrxTService.getHulvType(inDto);
        inDto.put("TYPE",hulvTypeDto.getString("TYPE"));
        inDto.put("GWEK001",resultDto.getString("GWAK008"));
        Dto mmrx009Dto = this.mmrxTService.getMmrx009(inDto);
        outDto.put("MMRX010",resultDto.getString("MMRX010")); //折扣率
        outDto.put("MMRX006",resultDto.getString("GWAK008")); //币种
        outDto.put("MMRX006_NAME",resultDto.getString("GWACL003")); //币种
        outDto.put("MMRX009",mmrx009Dto.getString("MMRX009")); //汇率
        outDto.put("MMRX007",resultDto.getString("MMRX007"));  //税种
        outDto.put("MMRX007_NAME",resultDto.getString("MMRX007_NAME"));  //税种名称
        outDto.put("GWEA006",resultDto.getString("GWEA006"));  //税率
        return ResponseData.success(outDto);
    }

    @RequestMapping("/getGwal")
    @ResponseBody
    public ResponseData getGwal(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = mmrxTService.listGwal(inDto);
        return ResponseData.success(result);
    }

    // 根据用户ID 获取归属法人和ERP 营运中心
    @RequestMapping("/getGwakData")
    @ResponseBody
    public ResponseData getGwakData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = mmrxTService.getGwakData(inDto);
        return ResponseData.success(result);
    }

    @RequestMapping("/selectGwakByKey")
    @ResponseBody
    public ResponseData selectGwakByKey(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = mmrxTService.selectGwakByKey(inDto);
        return ResponseData.success(result);
    }

    /**
     * 修改开票信息
     */
    @RequestMapping("/editGwak")
    @ResponseBody
    public ResponseData editGwak(GwakT gwakT) {
        mmrxTService.editGwak(gwakT);
        return SUCCESS_TIP;
    }

    /**
     * 修改地址信息
     */
    @RequestMapping("/editGwal")
    @ResponseBody
    public ResponseData editGwal(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        mmrxTService.editGwal(inDto);
        return SUCCESS_TIP;
    }
}
